﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
namespace WebApplication1
{
    public partial class AddMeeting1 : System.Web.UI.Page
    {
        MemberAL memberAL = new MemberAL();
        public Guid SelectedMember
        {
            get
            {
                if (Session["MemberId"] == null)
                {
                    return memberAL.GetAllMembers()[0].MemberId;
                }
                else
                {
                    return new Guid(Session["MemberId"].ToString());
                }
            }
            set
            {
                Session["MemberId"] = value;
            }
        }

        public Guid SelectedCommittee
        {
            get
            {
                if (Session["CommitteeId"] == null)
                {
                    return memberAL.GetAllCommittees()[0].CommitteeId;
                }
                else
                {
                    return new Guid(Session["CommitteeId"].ToString());
                }
            }
            set
            {
                Session["CommitteeId"] = value;
            }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            List<CommitteeDTO> committeeDTO = memberAL.GetAllCommittees();
            List<MemberDTO> memberDTO = memberAL.GetAllMembers();
            ddlMeetingCommitteeName.DataSource = committeeDTO;
            ddlMeetingCommitteeName.DataTextField = "Title";
            ddlMeetingCommitteeName.DataValueField = "CommitteeId";
            ddlMeetingCommitteeName.SelectedValue = SelectedCommittee.ToString();
            ddlMeetingCommitteeName.DataBind();


            ddlMeetingChairedBy.DataSource = memberDTO;
            ddlMeetingChairedBy.DataTextField = "FullName";
            ddlMeetingChairedBy.DataValueField = "MemberId";
            ddlMeetingChairedBy.SelectedValue = SelectedMember.ToString();
            ddlMeetingChairedBy.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnMeetingSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                MemberAL memberAL = new MemberAL();
                Guid committeeId = new Guid(ddlMeetingCommitteeName.SelectedValue);
                DateTime meetingHeldOn = DateTime.MinValue;
                int startYr = memberAL.GetCommitteeById(committeeId).SingleOrDefault().StartDate.Year;
                int endYr = memberAL.GetCommitteeById(committeeId).SingleOrDefault().EndDate.Year;
               
                if (txtMeetingHeldOn.Text=="")
                {
                    meetingHeldOn = Convert.ToDateTime("01/01/"+endYr);
                }
                else{
                
                    meetingHeldOn = Convert.ToDateTime(txtMeetingHeldOn.Text);
                }
                String title = "The " + ddlMeetingType.SelectedValue + " of the Exeutive Committee " + endYr + "/" + startYr + " held at  " + meetingHeldOn.ToShortDateString();
                MeetingDTO meetingTO = new MeetingDTO
                {
                    MeetingId = Guid.NewGuid(),
                    CommitteeId = new Guid(ddlMeetingCommitteeName.SelectedValue),
                    Type = ddlMeetingType.SelectedValue,
                    Title = title,
                    Description = txtMeetingDesc.Text,
                    ChairedBy = new Guid(ddlMeetingChairedBy.SelectedValue),
                    HeldAt = txtMeetingHeldAt.Text,
                    HeldOn = meetingHeldOn,
                    //ModifiedBy="Tharsan",
                    ModifiedTime = System.DateTime.Now
                };

               string result= memberAL.saveMeeting(meetingTO);
               if (result == "Saved")
               {
                   lblAddMeetingMsg.Text = "The Meeting Details added Successfully";
                   txtMeetingDesc.Text = "";
                   txtMeetingHeldAt.Text = "";
                   txtMeetingHeldOn.Text = "";


               }
               else if (result == "Updated")
               {
                   lblAddMeetingMsg.Text = "The Meeting Details Updated Successfully";
                   txtMeetingDesc.Text = "";
                   txtMeetingHeldAt.Text = "";
                   txtMeetingHeldOn.Text = "";
               }
               else if (result == "AlreadyExists")
               {
                   lblAddMeetingMsg.Text = "The Meeting Name is already exists for this Ex Committee";
                   txtMeetingDesc.Text = "";
                   txtMeetingHeldAt.Text = "";
                   txtMeetingHeldOn.Text = "";
               }
               else
               {
                   lblAddMeetingMsg.Text = "Your data cannot be added";
                   txtMeetingDesc.Text = "";
                   txtMeetingHeldAt.Text = "";
                   txtMeetingHeldOn.Text = "";
               }
            }
            catch (Exception ee)
            {
                lblAddMeetingMsg.Text = "There is an exception occured while inserting the Meeting details " + ee.ToString();
            }


        }
    }
}
